﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPHelper
{
	public class Timer
	{
		public enum Schedule
		{
			Every_5_Minutes,
			Every_30_Minutes,
			Every_1_Hour,
			Every_6_Hours,
			Every_12_Hours,

		}

		public static string FormatSchedule(Schedule schedule)
		{
			return schedule.ToString().Replace("_", " ");
		}

		/// <summary>
		/// Get all Timer Job Definitions
		/// </summary>
		/// <param name="service"></param>
		/// <returns></returns>
		public static List<SPJobDefinition> GetAllJobs(SPSite site)
		{
			var jobs = new List<SPJobDefinition>();
			try
			{
				if (site == null) { site = Site.GetSite(); }
				if (site == null) { throw new Exception("Failed to get SPSite from SPContext"); }

				jobs = GetAllJobs(site.WebApplication);
			}
			catch (Exception ex)
			{
				Log.WriteError("fgchjFkjlhgdxffx", ex);
				jobs = null;
			}
			return jobs;
		}

		/// <summary>
		/// Get all timer job definitions
		/// </summary>
		/// <param name="webApplication"></param>
		/// <returns></returns>
		public static List<SPJobDefinition> GetAllJobs(SPWebApplication webApplication)
		{
			var jobs = new List<SPJobDefinition>();
			try
			{
				if (webApplication == null) { throw new Exception("SPWebApplication passed is null"); }

				// get jobs
				if (webApplication.JobDefinitions != null && webApplication.JobDefinitions.Count > 0)
				{
					jobs.AddRange(webApplication.JobDefinitions);
				}
			}
			catch (Exception ex)
			{
				Log.WriteError("dfgcffdfGdgfdh", ex);
				jobs = null;
			}
			return jobs;
		}

		public static List<SPJobDefinition> GetAllJobs(SPFarm farm)
		{
			var jobs = new List<SPJobDefinition>();
			try
			{
				if (farm == null) { throw new Exception("SPFarm passed is null"); }

				// get services
				var services = Service.GetAllServices(farm);
				foreach (var service in services)
				{
					if (service.JobDefinitions != null && service.JobDefinitions.Count > 0)
					{
						jobs.AddRange(service.JobDefinitions);
					}
				}
			}
			catch (Exception ex)
			{
				Log.WriteError("rtukhgtfhdfdff", ex);
				jobs = null;
			}
			return jobs;
		}


		/// <summary>
		/// Get Timer Job Definition
		/// </summary>
		/// <param name="webApplication"></param>
		/// <param name="jobName"></param>
		/// <returns></returns>
		public static SPJobDefinition GetJob(SPWebApplication webApplication, string jobName)
		{
			try
			{
				if (webApplication == null) { throw new Exception("SPWebApplication passed is null"); }
				if (string.IsNullOrEmpty(jobName)) { throw new Exception("Job name passed is null"); }


				foreach (SPJobDefinition job in webApplication.JobDefinitions)
				{
					if (String.Compare(job.Name, jobName, true) == 0)
					{
						return job;
					}
				}
			}
			catch (Exception ex)
			{
				Log.WriteError("rt5yytyrerrrF", ex, "job name: " + jobName);
			}
			return null;
		}

		// EXAMPLE CustomJob
		//public class CustomJob : SPJobDefinition
		//{
		//    /// <summary>
		//    /// ctor
		//    /// </summary>
		//    public CustomJob() { }
		//    public CustomJob(SPWebApplication webApp, string jobName) : base(jobName, webApp, null, SPJobLockType.ContentDatabase)
		//    {
		//        this.Title = jobName;
		//    }

		//    public override void Execute(Guid targetInstanceId)
		//    {
				
		//    }
		//}
	}
}
